/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package e.odbo.data.dao;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.I_Session;
import e.odbo.data.dao.table.I_TableSentenceBuilder;
import e.odbo.data.bean.key.KeyBuilder;
import e.odbo.data.dao.table.TableDefinition;
import e.odbo.data.dsl.query.QBFParameter;
import e.odbo.data.dsl.query.QBFParameters;
import e.odbo.data.bean.IKeyed;
import e.odbo.data.bean.I_BaseBean;
import e.odbo.data.bean.I_ViewBean;
import e.odbo.data.sample.security.I_SecurityFilter;
import e.odbo.data.dsl.Select;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Deprecated
public abstract class DataLogic<D extends BaseDAO> implements I_DataLogic {
    protected I_Session s;
    protected I_SecurityFilter securityFilter;
    EntityManager entityManager;

    public DataLogic(I_Session s,I_TableSentenceBuilder builder) {
        this(new EntityManager(s,builder));
    }

    public DataLogic(EntityManager entityManager){
        this.s=entityManager.getSession();
        this.entityManager=entityManager;
        securityFilter=entityManager.getBuilder().getSecurityFilter();
    }

    public void addDaoToMap(ViewDAO obj) {
        entityManager.addDaoToMap(obj);
    }

    public ViewDAO getDao(Class c) {
        return entityManager.getDao(c);
    }

    public int insert(I_BaseBean obj) throws BasicException {
        return entityManager.insert(obj);
    }

    public IKeyed insertAutoGenerator(I_BaseBean obj) throws BasicException {
        insert(obj);
        return KeyBuilder.getBeanKey(obj);
    }

    public int update(I_BaseBean obj) throws BasicException {
        return entityManager.update(obj);
    }

    public int delete(I_BaseBean obj) throws BasicException {
        return entityManager.delete(obj);
    }

    public int insertOrUpdate(I_BaseBean obj) throws BasicException {
        return entityManager.insertOrUpdate(obj);
    }

    public int deleteByIKeyed(Class c, IKeyed k) throws BasicException {
        return entityManager.deleteByIKeyed(c,k);
    }

    public int deleteObjs(List objs) throws BasicException {
        return entityManager.deleteObjs(objs);
    }

    public int deleteByQBFParameters(Class c, QBFParameters filter) throws BasicException {
        return entityManager.deleteByQBFParameters(c,filter);
    }

    public int insertObjs(List objs) throws BasicException {
        return entityManager.insertObjs(objs);
    }

    public int updateObjs(List objs) throws BasicException {
        return entityManager.updateObjs(objs);
    }

    public int insertOrUpdate(List objs) throws BasicException {
        return entityManager.insertOrUpdate(objs);
    }


    public I_ViewBean find(Class c, IKeyed id) throws BasicException {
        return entityManager.find(c,id);
    }

    public I_ViewBean findByStringId(Class c, String id) throws BasicException {
        return find(c, KeyBuilder.getKey(id));
    }

    public I_ViewBean findByIntId(Class c, int id) throws BasicException {
        return find(c, KeyBuilder.getKey(id));
    }

    public List list(Class c) throws BasicException {
        return entityManager.list(c);
    }

    /**
     * object 参数应不用，用QBF
    public List listByFilter(Class c, Object filter) throws BasicException {
        if (daoMap.containsKey(c))
            return daoMap.get(c).list(filter);
        else if (infoDaoMap.containsKey(c))
            return infoDaoMap.get(c).list(filter);
        else if (viewDaoMap.containsKey(c))
            return viewDaoMap.get(c).list(filter);
        throw new BasicException("DAO no suport list:" + c);
    }**/

    @Override
    public List query(Class c, QBFParameters filter) throws BasicException {
       return entityManager.query(c,filter);
    }

    @Override
    public List queryByQBFParameter(Class c, QBFParameter filter) throws BasicException {
       return entityManager.queryByQBFParameter(c,filter);
    }

    public List listByParent(Class c, final I_ParentNode parent) throws BasicException {
        return entityManager.listByParent(c,parent);
    }

    public List list(Class c, final Class parent, final String id) throws BasicException {
        return entityManager.list(c,parent,id);
    }

    public List listBySelect(Class c,Select select)throws BasicException{
        return entityManager.listBySelect(c,select);
    }

    @Override
    public int updateByFieldWithIKeyed(IKeyed obj, String field, Object newValue) throws BasicException {
        return entityManager.updateByFieldWithIKeyed(obj, field, newValue);
    }

    @Override
    public int updateByField(Class c, IKeyed obj, String field, Object newValue) throws BasicException {
        return entityManager.updateByField(c,obj, field, newValue);
    }

    public TableDefinition getTableDefinition(Class c) {
        return entityManager.getTableDefinition(c);
    }

    public I_Session getSession() {
        return s;
    }

    public int clear(Class c) throws BasicException {
        return entityManager.clear(c);
    }

    /**
    @Override
    public List listByFilterWithClassName(String c, Object filter)
            throws BasicException {
        Class cs;
        try {
            cs = Class.forName(c);
        } catch (ClassNotFoundException e) {
            throw new BasicException("DAO no suport updateByField:" + c);
        }
        return listByFilter(cs, filter);
    }**/

    @Override
    public List listWithClassName(String c) throws BasicException {
        return entityManager.listWithClassName(c);
    }

    @Override
    public List listWithParentClassAndKey(String c, String parent, String id)
            throws BasicException {
        return entityManager.listWithParentClassAndKey(c,parent,id);
    }

    @Override
    public List queryByQBFParameters(String c, QBFParameters filter) throws BasicException {
        Class cs;
        try {
            cs = Class.forName(c);
        } catch (ClassNotFoundException e) {
            throw new BasicException("DAO no suport updateByField:" + c);
        }
        return query(cs, filter);
    }

    public abstract Class transParentClass(Class in);

    @Override
    public List listWithDateTime(Class c, Date start, Date end)
            throws BasicException {
        return entityManager.listWithDateTime(c,start,end);
    }

    @Override
    public List listWithDateTimeByClassName(String c, Date start, Date end)
            throws BasicException {
        Class cs;
        try {
            cs = Class.forName(c);
        } catch (ClassNotFoundException e) {
            throw new BasicException("DAO no suport updateByField:" + c);
        }
        return listWithDateTime(cs, start, end);
    }

    @Override
    public boolean isExistByField(Class c, String field, Object value)
            throws BasicException {
        return entityManager.isExistByField(c,field, value);
    }

    public boolean isExistByFields(Class c, String[] fields, Object[] values)
            throws BasicException {
        return entityManager.isExistByFields(c,fields, values);
    }
}
